<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/12/14
 * Time: 下午4:44
 */
class WeChat
{
    const APP_ID = 'wx664e3e4c6b3b1267';
    const APP_SECRET = '688351e34b6b6b4a46e7bf3c32cdf8a9';

    /*
     * 获取微信token
     * 中控服务器应根据 expires_in 保存token
     * 控制统一刷新有效期
     * */
    public static function get_access_token(){
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='. WeChat::APP_ID .'&secret=' . WeChat::APP_SECRET;
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }

    /*
     * 获取微信回调服务器IP地址，用于业务服务器IP白名单使用
     * */
    public static function get_callback_ip($access_token){
        $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $access_token;
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }

    /*
     * 网页授权
     * 用于获取临时用户访问权限和用户openid
     * */
    public static function get_oauth_access_token($code){
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . WeChat::APP_ID . '&secret=' . WeChat::APP_SECRET . '&code=' . $code . '&grant_type=authorization_code';
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }

    /*
     * 刷新用户临时授权
     * 用于延长用户临时授权可用时间
     * */
    public static function refresh_oauth_access_token($refresh_token){
        $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . WeChat::APP_ID . '&grant_type=refresh_token&refresh_token=' . $refresh_token;
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }


    /*
     * 检查临时授权是否有效
     * */
    public static function check_oauth_access_token($access_token,$openid){
        $url = 'https://api.weixin.qq.com/sns/auth?access_token=' . $access_token . '&openid=' . $openid;
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }

    /*
     * 获取用户信息
     * openID和附属信息
     * */
    public static function get_user_info($access_token,$openid){
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
        $response = Httpful::get($url)
            ->timeoutIn(5)
            ->sendsForm()->send();
        return $response;
    }

    public static function send_message($access_token,$open_id,$message_modul_id,$link_url,$data){
        $request = array(
            'touser' => $open_id,
            'template_id' => 'ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY',
            'url' => 'http://www.baidu.com',
            'topcolor' => '#FF0000',
            'data' => array(
                'User' => array(
                    'value' => '黄先生',
                    'color' => '#173177'
                ),
                'Date' => array(
                    'value' => '06月07日 19时24分',
                    'color' => '#173177'
                ),
                'CardNumber' => array(
                    'value' => '0426',
                    'color' => '#173177'
                ),
                'Type' => array(
                    'value' => '消费',
                    'color' => '#173177'
                ),
                'Money' => array(
                    'value' => '人民币260.00元',
                    'color' => '#173177'
                ),
                'DeadTime' => array(
                    'value' => '06月07日19时24分',
                    'color' => '#173177'
                ),
                'Left' => array(
                    'value' => '黄先生',
                    'color' => '#173177'
                ),
            )
        );

        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
        $response = Httpful::post($url)
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();
        return $response;
    }
}